﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.OleDb;
using System.Data;
using System.Runtime.InteropServices;
using System.Drawing.Printing;
using Microsoft.Win32;

namespace dianying
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            webBrowser1.NewWindow += new CancelEventHandler(webBrowser1_NewWindow);

        }

        // public OleDbConnection sqlConnection1 = null;
        public OleDbCommand sqlCommand1 = null;
        public OleDbConnection sqlConnection1 = new OleDbConnection("Data Source=qggdch.mdb;Jet OLEDB:Engine Type=5;Provider=Microsoft.Jet.OLEDB.4.0;");
        // public OleDbConnection sqlConnection1 = null ;
        public static string LocalIP = string.Empty;
        public static string RemoteIP = string.Empty;
        public static string LocalMAC = string.Empty;
        public static string stringkey = "";
        public static int number = 0;
        public static int duandian = 0;
        public static bool Debug = false;
        public static string sq = "未授权";
        public static string urlna = "没有";
        List<string> listurl = new List<string>();
        public int urln = 0;
        /// <summary>
        /// 获取ｕｒｌ　连接
        /// </summary>
        /// <returns></returns>
        public string GetUrl()
        {
            string rolename = null;
            try
            {
                sqlCommand1 = new OleDbCommand();
                sqlCommand1.Connection = sqlConnection1;
                // string sql="delete * from outinfo where OutID="+ds.Tables["out"].Rows[dataGrid1.CurrentCell.RowNumber][13].ToString().Trim()+"";
                sqlConnection1.Open();
                OleDbCommand cmd = new OleDbCommand("", sqlConnection1);
                string sql = "select urlname from urlinfo where url='" + comboBox1.Text.ToString() + "' ";//+ password.Text.Trim() + "'";
                //   string 
                cmd.CommandText = sql;

                if (null != cmd.ExecuteScalar())
                {

                    rolename = cmd.ExecuteScalar().ToString();
                    if (rolename == "D19E489C25E4555F63EE5FB9B5E1A94B")
                    {
                        Debug = true;
                    }
                    else
                    {
                        // rolename = rolename;
                        // rolename = DESEncrypt.Decrypt(rolename);
                    }

                }

                sqlConnection1.Close();
            }
            catch (Exception)
            {

                MessageBox.Show("可能缺少文件qggdch.mdb文件，请联系技术吖柒");
            }

            return rolename;
        }
        public string intUrl()
        {
            string rolename = null;

            try
            {

                sqlCommand1 = new OleDbCommand();
                sqlCommand1.Connection = sqlConnection1;
                // string sql="delete * from outinfo where OutID="+ds.Tables["out"].Rows[dataGrid1.CurrentCell.RowNumber][13].ToString().Trim()+"";
                sqlConnection1.Open();
                OleDbCommand cmd = new OleDbCommand("", sqlConnection1);
                // string sql = "select urlname from urlinfo where url='" + comboBox1.Text.ToString() + "' ";//+ password.Text.Trim() + "'";
                string sql = "update urlinfo set urlname='" + textBox1.Text + "'" + "where name='30'";
                //   string 
                cmd.CommandText = sql;

                if (null != cmd.ExecuteScalar())
                {
                    rolename = cmd.ExecuteScalar().ToString();
                }

                sqlConnection1.Close();
            }
            catch (Exception)
            {

                MessageBox.Show("可能缺少文件qggdch.mdb文件，请联系技术吖柒");
            }

            return rolename;
        }
        public string LSUrl()
        {
            string rolename = null;

            try
            {

                sqlCommand1 = new OleDbCommand();
                sqlCommand1.Connection = sqlConnection1;
                // string sql="delete * from outinfo where OutID="+ds.Tables["out"].Rows[dataGrid1.CurrentCell.RowNumber][13].ToString().Trim()+"";
                sqlConnection1.Open();
                OleDbCommand cmd = new OleDbCommand("", sqlConnection1);
                // string sql = "select urlname from urlinfo where url='" + comboBox1.Text.ToString() + "' ";//+ password.Text.Trim() + "'";
                string sql = "update urlinfo set urlname='" + textBox1.Text + "'" + "where name='11'";
                //   string 
                cmd.CommandText = sql;

                if (null != cmd.ExecuteScalar())
                {
                    rolename = cmd.ExecuteScalar().ToString();
                }

                sqlConnection1.Close();
            }
            catch (Exception)
            {

                MessageBox.Show("可能缺少文件qggdch.mdb文件，请联系技术吖柒");
            }

            return rolename;
        }


        public string LUrl(int maa = 0)
        {
            string rolename = null;

            try
            {

                sqlCommand1 = new OleDbCommand();
                sqlCommand1.Connection = sqlConnection1;
                // string sql="delete * from outinfo where OutID="+ds.Tables["out"].Rows[dataGrid1.CurrentCell.RowNumber][13].ToString().Trim()+"";
                sqlConnection1.Open();
                OleDbCommand cmd = new OleDbCommand("", sqlConnection1);
                string sqla = "select urlname from urlinfo where url='通用解析11' ";//+ password.Text.Trim() + "'";
                if (maa == 1)
                {
                    string sql = "update urlinfo set urlname='" + urlna + "'" + "where url='通用解析11'";
                    cmd.CommandText = sql;
                }
                // cmd.ExecuteNonQuery();
                cmd.CommandText = sqla;
                rolename = cmd.ExecuteScalar().ToString();
                sqlConnection1.Close();
            }
            catch (Exception)
            {

                MessageBox.Show("授权有问题，请联系技术吖柒");
            }

            return rolename;
        }

        public static string GetUrlsq()
        {
            string rolename = null;
            OleDbCommand sqlCommand1 = null;
            OleDbConnection sqlConnection1 = new OleDbConnection("Data Source=qggdch.mdb;Jet OLEDB:Engine Type=5;Provider=Microsoft.Jet.OLEDB.4.0;");

            try
            {

                sqlCommand1 = new OleDbCommand();
                sqlCommand1.Connection = sqlConnection1;
                // string sql="delete * from outinfo where OutID="+ds.Tables["out"].Rows[dataGrid1.CurrentCell.RowNumber][13].ToString().Trim()+"";
                sqlConnection1.Open();
                OleDbCommand cmd = new OleDbCommand("", sqlConnection1);
                string sql = "select urlname from urlinfo where url='通用解析30'";//+ password.Text.Trim() + "'";
                //   string 
                cmd.CommandText = sql;

                if (null != cmd.ExecuteScalar())
                {
                    rolename = cmd.ExecuteScalar().ToString();
                    if (rolename == "D19E489C25E4555F63EE5FB9B5E1A94B")
                    {
                        Debug = true;
                    }
                    else
                    {
                        rolename = DESEncrypt.Decrypt(rolename);
                        rolename = DESEncrypt.Decrypt(rolename);
                        rolename = DESEncrypt.Decrypt(rolename);
                    }
                }
                sqlConnection1.Close();
            }
            catch (Exception)
            {
                MessageBox.Show("没有获取到授权信息，请联系技术吖柒");
            }

            return rolename;
        }

        private void button1_Click_2(object sender, EventArgs e)
        {
            if (!string.IsNullOrEmpty(textBox1.Text))
            {
                string url = GetUrl() + textBox1.Text.ToString().Trim();

                geturl(url);
            }
            else
            {
                MessageBox.Show("输入你要解析的会员视频连接");
            }


        }

        public void geturl(string url)
        {
            var mac = NetWork.GetMACs(out LocalIP);
            string getmac = Convert.ToString(mac.ToString());
            getmac = mac[0].ToString();
            if (number < 0)
            {
                this.Close();
            }
            if (number < 10 || getNum() == true || Debug == true)
            {
                if (getNum() == true && GetUrlsq() == getmac || Debug == true)
                {
                    if (!string.IsNullOrEmpty(comboBox1.Text))
                    {
                        LUrl(1);
                        number++;
                        //  string url = null;
                        // url = GetUrl() + textBox1.Text.ToString().Trim();
                        webBrowser1.Url = new Uri(url);

                    }
                }
                else if (number < 10 || Debug == true)
                {
                    if (!string.IsNullOrEmpty(comboBox1.Text))
                    {
                        LUrl(1);
                        number++;
                        //  string url = null;
                        // url = GetUrl() + textBox1.Text.ToString().Trim();
                        webBrowser1.Url = new Uri(url);

                    }
                    else
                    {
                        MessageBox.Show("还没有选择解析通道");
                    }


                }
                // System.IO.StreamReader sr = new System.IO.StreamReader(webBrowser1.DocumentStream, Encoding.GetEncoding("gb2312"));

            }
            else if (number < 10 && comboBox1.Text.ToString() == "通用解析2")
            {
                // System.IO.StreamReader sr = new System.IO.StreamReader(webBrowser1.DocumentStream, Encoding.GetEncoding("gb2312"));
                if (!string.IsNullOrEmpty(comboBox1.Text))
                {

                    number++;
                    // string url = null;
                    //  url = GetUrl() + textBox1.Text.ToString().Trim();
                    webBrowser1.Url = new Uri(url);
                    LUrl(1);

                }
                else
                {
                    MessageBox.Show("还没有选择解析通道");
                }
            }
            else if (comboBox1.Text.ToString() == "通用解析10")
            {
                // System.IO.StreamReader sr = new System.IO.StreamReader(webBrowser1.DocumentStream, Encoding.GetEncoding("gb2312"));
                if (!string.IsNullOrEmpty(comboBox1.Text))
                {

                    number++;
                    // string url = null;
                    webBrowser1.Url = new Uri(url);
                    LUrl(1);

                }
                else
                {
                    MessageBox.Show("还没有选择解析通道");
                }
            }
            else
            {
                MessageBox.Show("授权次数已经没有，请联系作者授权");
                this.Close();
            }


        }
        public static bool getNum()
        {
            bool num = false;
            List<string> mac = NetWork.GetMACs(out LocalIP);
            string getmac = mac[0].ToString();
            if (GetUrlsq() == getmac)
            {
                //  MessageBox.Show("已经是授权软件可以随意切换通道");
                num = true;
                sq = "已经授权";
            }
            else if (Debug == true)
            {
                num = true;
                sq = "无敌状态";
            }
            else
            {
                MessageBox.Show("不是授权软件只能使用通道2");
                num = false;
                sq = "未授权";
            }
            return num;
        }
        /// <summary>
        /// 获取urlname列表
        /// </summary>
        /// <returns></returns>
        public List<string> Urlinfo()
        {
            List<string> rolename = null;
            try
            {
                sqlCommand1 = new OleDbCommand();
                sqlCommand1.Connection = sqlConnection1;
                sqlConnection1.Open();
                string sql = "select * from urlinfo ";//+ password.Text.Trim() + "'";
                OleDbCommand cmd = new OleDbCommand(sql, sqlConnection1);
                OleDbDataReader myReader = cmd.ExecuteReader();
                int i = 0;
                List<string> urlname = new List<string>();
                // urlname = null;
                while (myReader.Read())
                {

                    string aa = myReader.GetValue(1).ToString();
                    urlname.Add(aa.ToString());
                }
                rolename = urlname;
                sqlConnection1.Close();
            }
            catch (Exception)
            {

                MessageBox.Show("可能缺少文件qggdch.mdb文件，请联系技术吖柒");
            }

            return rolename;

        }
        private void Form1_Load(object sender, EventArgs e)
        {
            var mac = NetWork.GetMACs(out LocalIP);
            string getmac = Convert.ToString(mac.ToString());
            getmac = mac[0].ToString();
            if (GetUrlsq() == getmac)
            {
                sq = "已经授权";
                MessageBox.Show("已经是授权软件可以随意切换通道");
            }
            else if (Debug == true)
            {
                sq = "无敌状态";
                MessageBox.Show("恭喜你进入无敌状态，可以随意切换通道");

            }
            else
            {
                MessageBox.Show("不是授权软件只能使用通道10，并且使用次数为100");
                sq = "未授权";
                number = 10;

            }
            this.Text = "齐卓影视解析器：(" + sq + ")";
            try
            {

                List<string> urlname = Urlinfo();

                for (int i = 0; i < urlname.Count - 1; i++)
                {
                    comboBox1.Items.Add(urlname[i]);
                }
            }
            catch (Exception)
            {

                // throw;
            }
            /*
             * RegistryKey hklm = Application.LocalMachine;
 RegistryKey run = hklm.CreateSubKey(@"SOFTWARE\Microsoft\Windows\Current\Version\Run");
 try
 {
 run.SetValue("tractor.exe","D:\\tractor.exe");
 MessageBox.Show("注册表添加成功!!","提示",MessageBoxButton.OK, MessageBoxIcon.Information);
hklm.Close();
}
catch(Exception ee)
{
MessageBox.Show(my.Message.ToString(),"提示",MessageBoxButton.OK, MessageBoxIcon.Error);
}

             */


            //try
            //{

            //    //    HKEY_CURRENT_USER\Software\Microsoft\Internet Explorer\Main\FeatureControl\FEATURE_BROWSER_EMULATION
            //    ///HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Internet Explorer\MAIN\FeatureControl\FEATURE_BROWSER_EMULATION
            //    //   RegistryKey r = Registry.CurrentUser.OpenSubKey("Software\\Microsoft\\Windows\\CurrentVersion\\Policies\\System", true);
            //    //    r.SetValue("DisableTaskMgr", "1"); //屏蔽任务管理器 
            //    // RegistryKey r = Registry.CurrentUser.OpenSubKey(@"HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Internet Explorer\MAIN\FeatureControl\FEATURE_BROWSER_EMULATION", true);
            //    RegistryKey rr = Registry.LocalMachine.OpenSubKey("SOFTWARE\\Microsoft\\Internet Explorer\\MAIN\\FeatureControl\\FEATURE_BROWSER_EMULATION", true);
            //    if (rr.GetValue("dianying.exe") != null) //读取失败返回null
            //    {
            //        string aa = rr.GetValue("dianying.exe").ToString();
            //        // aa = rr.GetValue();
            //    }
            //    rr.SetValue("dianying.exe", "7000"); //屏蔽任务管理器 
            //    rr.Close();
            //}
            //catch
            //{
            //    // RegistryKey r = Registry.CurrentUser.CreateSubKey("Software\\Microsoft\\Windows\\CurrentVersion\\Policies\\System");
            //    // r.SetValue("DisableTaskMgr", "0");
            //    //   r.DeleteValue("DisableTaskMgr");
            //}

        }

        private void 优酷ToolStripMenuItem_Click(object sender, EventArgs e)
        {

            duandian++;
            webBrowser1.Url = new Uri("http://www.youku.com/");
            number = 0;


        }

        private void 爱奇艺ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            webBrowser1.Url = new Uri("http://www.iqiyi.com/");
            number = 0; duandian++;
        }

        private void 腾讯视频ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            webBrowser1.Url = new Uri("https://v.qq.com/");
            number = 0; duandian++;

        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            var mac = NetWork.GetMACs(out LocalIP);
            string getmac = Convert.ToString(mac.ToString());
            getmac = mac[0].ToString();
            if (Debug != true)
            {
                if (GetUrlsq() != getmac)
                {
                    if (duandian > 10 || duandian > 10)
                    {
                        MessageBox.Show("请联系作者授权");

                        this.Close();
                        return;

                    }
                    if (duandian > 11 || duandian > 11)
                    {
                        MessageBox.Show("请联系作者授权");

                        this.Close();
                        return;
                    }
                }
                if (!getNum())
                {
                    if (duandian > 10 || duandian > 10)
                    {
                        MessageBox.Show("请联系作者授权");

                        this.Close();
                        return;

                    }
                    if (duandian > 11 || duandian > 11)
                    {
                        MessageBox.Show("请联系作者授权");

                        this.Close();
                        return;
                    }
                }
            }
        }

        private void webBrowser1_NewWindow(object sender, CancelEventArgs e)
        {
            e.Cancel = true;
            webBrowser1.Navigate(webBrowser1.StatusText);
        }

        private void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
        {
            //将所有的链接的目标，指向本窗体
            //foreach ( HtmlElement archor in this.webBrowser1.Document.Links)
            //{
            //    archor.SetAttribute("target", "_self");
            //}

            ////将所有的FORM的提交目标，指向本窗体
            //foreach (HtmlElement form in this.webBrowser1.Document.Forms)
            //{
            //    form.SetAttribute("target", "_self");
            //}

            //((WebBrowser)sender).Document.Window.Error += new HtmlElementErrorEventHandler(Window_Error);  
            bool cun = false;

            if (number == 0)
            {
                urlna = webBrowser1.Url.ToString();
            }
            if (listurl.Count == 0 || listurl.Count == 1)
            {
                urln++;
                listurl.Add(webBrowser1.Url.ToString());
            }
            else
            {
                for (int i = 0; i < listurl.Count; i++)
                {
                    if (listurl[i].ToString() == webBrowser1.Url.ToString())
                    {
                        cun = true;
                    }
                }
                if (cun == false)
                {
                    urln++;
                    listurl.Add(webBrowser1.Url.ToString());
                }

            }
        }
        private void Window_Error(object sender, HtmlElementErrorEventArgs e)
        {
            // Ignore the error and suppress the error dialog box.   
            e.Handled = true;
        }


        private void button2_Click(object sender, EventArgs e)
        {
            string url = GetUrl() + urlna.ToString();
            geturl(url); duandian++;
        }

        private void 芒果ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            webBrowser1.Url = new Uri("http://www.mgtv.com/");
            number = 0; duandian++;
        }

        private void button4_Click(object sender, EventArgs e)
        {
            webBrowser1.Url = new Uri(urlna);
            number = 0; duandian++;
        }

        private void button3_Click(object sender, EventArgs e)
        {
            webBrowser1.Url = new Uri(LUrl()); number = 0; duandian++;
        }

        private void 土豆ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            webBrowser1.Url = new Uri("http://www.tudou.com/");
            number = 0; duandian++;

        }

        private void 乐视ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            webBrowser1.Url = new Uri("http://www.le.com/");
            number = 0; duandian++;
        }

        private void button5_Click(object sender, EventArgs e)
        {
            //System.Diagnostics.Process.Start
            webBrowser1.Url = new Uri(" http://www.baidu.com/s?wd=" + textBox2.Text.ToString().Trim());
            number = 0; duandian++;
        }

        private void 上一级(object sender, EventArgs e)
        {
            if (urln == listurl.Count)
            {
                urln--;
            }
            if (urln < listurl.Count && urln > 0)
            {
                webBrowser1.Url = new Uri(listurl[urln - 1]);
                urln--;
                number = 0;
            }
        }

        private void 下一级(object sender, EventArgs e)
        {
            if (urln < listurl.Count + 1 && urln > 0)
            {
                //for (int i = 0; i < listurl.Count - 1; i++)
                //{

                //}
                try
                {
                    webBrowser1.Url = new Uri(listurl[urln + 1]);
                    urln++;
                    number = 0;
                }
                catch (Exception)
                {

                    MessageBox.Show("已经没有下一级");
                }
            }
        }
        
        private void 赞助ToolStripMenuItem(object sender, EventArgs e)
        {
            Form3 form = new Form3();
            form.Show();
        }

        private void 授权ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Form2 form = new Form2();
            form.Show();
        }

        private void toolStripMenuItem2_Click(object sender, EventArgs e)
        {
            Form3 form = new Form3();
            form.Show();
        }


    }


}
